const express = require("express")
const router = express.Router()
const { default: mongoose } = require("mongoose")
const { readFile, wtiteFile } = require("../util/filedata")

// 获取schema
const { Schema } = mongoose
const url = "mongodb://127.0.0.1:27017/shopApp"
mongoose.connect(url).then(() => {
    // console.log("链接成功数据库")
})


// 创建数据模版
const productSchema = new Schema({
    data: { type: Array }

})
const product = mongoose.model("product", productSchema)
// 将json数据保存到数据库
async function saveBannerData() {
    // 读取数据
    /* let productData = JSON.parse(await readFile("./data/type.json", "utf8"));
    console.log("111", productData)
    // 保存到数据库
    let result = await product.create({
        product: productData.data
    });
    console.log("保存到数据库", result); */
    let result = product.find({}, (res) => {
        console.log('用户列表', res);
    })
}
// 仅执行一次
// saveBannerData();
router.get("/getList", async (req, res, next) => {
    try {
        let data = await product.find({}, { product })
        console.log("data数据", data);
        return res.status(200).json({
            msg: "okok",
            code: 200,
            data: data
        })
    }
    catch (err) {
        return next({
            code: 1001,
            error: err.message,
            errDic: 'hhhhhhhhh'
        })
    }
})

module.exports = router